/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */

// list.js
import { PNT, setNavBarTitle, formatTime, scrollLoadList } from '../../../../utils/utils';
import { GetCommentList } from '../../../../utils/apis';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    commentState: 0,
    goodsId: null,
    //commentLst: '',
    commentLst: [
      // {
      //   author: {
      //     avatar: '/images/default_avatar.png',
      //     username: '张三'
      //   },
      //   grade: 3,
      //   updated_at: '2019-10-11',
      //   content: '商品不错很好，赞一个',
      //   hf: {
      //     username: '李四',
      //     content: '嘎嘎嘎嘎嘎过过过过过'
      //   },
      //   thumbs: [
      //     "/images/info_help.png",
      //     "/images/info_help.png"

      //   ]
      // },

      // {
      //   author: {
      //     avatar: '/images/default_avatar.png',
      //     username: '李四'
      //   },
      //   grade: 5,
      //   updated_at: '2019-10-11 10:20:10',
      //   content: '商品不错很好，赞一个啦啦啦啦啦'
      // }
    ],
    pages: {
      page: 1,
      size: 10,
      total: 10,
      done: false,
      loading: false
    },
    page:0,
    flag:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    setNavBarTitle(PNT.comments.list);

    this.setData({
      goodsId: opt.goodsId,
    });
    this.getCommentLst();
  },

  /**
   * 评论列表
   * @author darlang
   */
  getCommentLst() {
    if (!this.data.flag) {
      showToast('数据加载中','none');
      return false;
    }
    wx.showLoading({ title: '加载中...', mask: true });
    var data = {};
    data.goods_id = this.data.goodsId;
    data.page = this.data.page;
    data.server = parseInt(this.data.commentState);
    GetCommentList(data).then(res => {
      var list = this.data.commentLst;
      if(res.error_code == 10000){
        res.data.comment.map(res=>{
          list.push(res);
        })
        this.setData({
          info:res.data,
          commentLst:list,
        })
      }else{
        showToast('数据已加载完毕','none');
        return;
      }
      this.setData({
        flag: true
      });
    });
  },

  /**
   * 切换评论
   * @author darlang
   */
  changeCommentState(e) {
    let commentState = e ? e.currentTarget.dataset.id : this.data.commentState;
    this.setData({
      flag: true,
      page: 0,
      commentLst: [],
      commentState: commentState
    });
    this.getCommentLst();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.changeCommentState();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.flag) {
      wx.showLoading({ title: '加载中...', mask: true });
    }
    var page = this.data.page;
    page = page + 1;
    this.setData({
      page: page,
      flag: false,
    })
    this.getCommentLst();
  }
});